//
//  HACChartUtils.m
//  Pods
//
//  Created by Hotacool on 2017/8/30.
//
//

#import "HACChartUtils.h"

@implementation HACChartUtils

#pragma mark ----- Tools

+ (void)drawLine:(CGContextRef)gc pt1:(CGPoint)pt1 pt2:(CGPoint)pt2 clr:(UIColor *)clr width:(float)width {
    CGContextSetStrokeColorWithColor(gc, clr.CGColor);
    CGContextMoveToPoint(gc, pt1.x, pt1.y);
    CGContextAddLineToPoint(gc, pt2.x, pt2.y);
    CGContextSetLineWidth(gc, width);
    CGContextStrokePath(gc);
}

+ (void)drawLine:(CGContextRef)gc pts:(CGPoint *)pts count:(int)icount clr:(UIColor *)clr width:(float)width {
    CGContextSetStrokeColorWithColor(gc, clr.CGColor);
    CGContextSetLineWidth(gc, width);
    CGContextAddLines(gc, pts, icount);
    CGContextStrokePath(gc);
}

+ (void)drawStrInRect:(NSString *)str rect:(CGRect)rect font:(UIFont *)font color:(UIColor *)color alignment:(NSTextAlignment)alignment {
    [color set];
    NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    paragraphStyle.lineBreakMode = NSLineBreakByClipping;
    paragraphStyle.alignment = alignment;
    [str drawInRect:rect withAttributes:@{NSFontAttributeName:font,
                                          NSForegroundColorAttributeName : color,
                                          NSParagraphStyleAttributeName: paragraphStyle}];
}

+ (void)drawAttributedStrInRect:(NSAttributedString *)str rect:(CGRect)rect {
    [str drawInRect:rect];
}

+ (void)drawLinearGradient:(CGContextRef)context
                        path:(CGPathRef)path
                  startColor:(CGColorRef)startColor
                    endColor:(CGColorRef)endColor {
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGFloat locations[] = { 0.0, 1.0 };
    NSArray *colors = @[(__bridge id) startColor, (__bridge id) endColor];
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations);
    CGRect pathRect = CGPathGetBoundingBox(path);
    //具体方向可根据需求修改 可改变方向是的渐变颜色45°或者垂直方向渐变颜色
    CGPoint startPoint = CGPointMake(CGRectGetMidX(pathRect), CGRectGetMinY(pathRect));
    CGPoint endPoint = CGPointMake(CGRectGetMidX(pathRect), CGRectGetMaxY(pathRect));
    CGContextSaveGState(context);
    CGContextAddPath(context, path);
    CGContextClip(context);
    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
    CGContextRestoreGState(context);
    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorSpace);
}

+ (NSString *)transferVolumeToStandardString:(long double)num {
    if (num < 0.01 && num > -0.01 ) {
        return @"0.00";
    }
    if (fabsl(num) > 100000000000)//单位用千亿，保留两位小数
    {
        return [NSString stringWithFormat:@"%.2Lf千亿",num/100000000000.0];
    }
    else if (fabsl(num) > 100000000)//单位用亿，保留两位小数
    {
        return [NSString stringWithFormat:@"%.2Lf亿",num/100000000.0];
    }
    else if (fabsl(num) > 10000)//单位用万，保留两位小数
    {
        return [NSString stringWithFormat:@"%.2Lf万",num/10000.0];
    }
    else {
        return [NSString stringWithFormat:@"%.0Lf", num];
    }
    
    return @"0.00";
}
@end
